package com.kly.manager.auth;

import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.json.JSONUtil;
import com.kly.domain.entity.Space;
import com.kly.domain.entity.SpaceUser;
import com.kly.domain.entity.User;
import com.kly.domain.enums.SpaceRoleEnum;
import com.kly.domain.enums.SpaceTypeEnum;
import com.kly.manager.auth.model.SpaceUserAuthConfig;
import com.kly.manager.auth.model.SpaceUserRole;
import com.kly.service.SpaceUserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * @author admin
 * @date 2025/3/19
 */
@Component
public class SpaceUserAuthManage {

    private static final SpaceUserAuthConfig SPACE_USER_AUTH_CONFIG;

    @Resource
    private SpaceUserService spaceUserService;

    static {
        String json = ResourceUtil.readUtf8Str("biz/SpaceUserAuthConfig.json");
        SPACE_USER_AUTH_CONFIG = JSONUtil.toBean(json, SpaceUserAuthConfig.class);
    }


    /**
     * 根据角色获取权限列表
     */
    public static List<String> getPermissionByRole(String role) {
        if (role == null) {
            return Collections.emptyList();
        }
        SpaceUserRole userRole = SPACE_USER_AUTH_CONFIG.getRoles().stream()
                .filter(spaceUserRole -> spaceUserRole.getKey().equals(role))
                .findFirst()
                .orElse(null);
        if (userRole == null) {
            return Collections.emptyList();
        }
        return userRole.getPermissions();
    }

    /**
     * 获取登录用户权限
     */
    public List<String> getPermissionList(Space space, User loginUser) {
        if (loginUser == null) {
            return Collections.emptyList();
        }
        //     管理员权限
        List<String> adminPermission = getPermissionByRole(SpaceRoleEnum.ADMIN.getValue());

        if (space == null) {
            //     判断是否是管理员
            if (SpaceRoleEnum.ADMIN.getValue().equals(loginUser.getUserRole())) {
                return adminPermission;
            } else {
                return Collections.emptyList();
            }
        }

        //     判断类型
        SpaceTypeEnum spaceTypeEnum = SpaceTypeEnum.getEnumByLevel(space.getSpaceType());
        if (spaceTypeEnum == null) {
            return Collections.emptyList();
        }

        switch (spaceTypeEnum) {
            case PRIVATE:
                // 私人和管理员具有权限
                if (space.getUserId().equals(loginUser.getId()) || SpaceRoleEnum.ADMIN.getValue().equals(loginUser.getUserRole())) {
                    return adminPermission;
                } else {
                    return Collections.emptyList();
                }
            case TEAMED:
                SpaceUser spaceUser = spaceUserService.lambdaQuery()
                        .eq(SpaceUser::getSpaceId, space.getId())
                        .eq(SpaceUser::getUserId, loginUser.getId())
                        .one();
                if (spaceUser != null) {
                    return getPermissionByRole(spaceUser.getSpaceRole());
                } else {
                    return Collections.emptyList();
                }

        }
        return Collections.emptyList();
    }
}
